package com.fuqi.android.zhongcai.base.view.widget;



import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.fuqi.android.zhongcai.R;
import com.fuqi.android.zhongcai.base.model.BasicItem;
import com.fuqi.android.zhongcai.base.model.IHashItem;
import com.fuqi.android.zhongcai.base.model.ViewItem;
import java.util.ArrayList;
import java.util.List;

public class HashView extends LinearLayout {

	private int mIndexController = 0;
	private LayoutInflater mInflater;
	private LinearLayout mMainContainer;

	private LinearLayout mListContainer;
	private List<IHashItem> mItemList;
	private ClickListener mClickListener;

	public HashView(Context context, AttributeSet attrs) {
		super(context, attrs);
		mItemList = new ArrayList<IHashItem>();
		mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		mMainContainer = (LinearLayout) mInflater.inflate(R.layout.hash_list_container, null);
		LayoutParams params = new LayoutParams(
				ViewGroup.LayoutParams.MATCH_PARENT,
				ViewGroup.LayoutParams.MATCH_PARENT);
		addView(mMainContainer, params);
		mListContainer = (LinearLayout) mMainContainer.findViewById(R.id.list_container);
	}

	/**
	 * 
	 * @param title
	 */
	public void addBasicItem(String title) {
		mItemList.add(new BasicItem(title));
	}

	/**
	 * 
	 * @param title
	 * @param summary
	 */
	public void addBasicItem(String title, String summary) {
		mItemList.add(new BasicItem(title, summary));
	}

	/**
	 * 
	 * @param title
	 * @param summary
	 * @param color
	 */
	public void addBasicItem(String title, String summary, int color) {
		mItemList.add(new BasicItem(title, summary, color));
	}

	/**
	 * 
	 * @param title
	 */
	public void addBasicItem(int drawableLeft, String title, String value, int drawableRight, String remider) {
		mItemList.add(new BasicItem(drawableLeft, title, value, drawableRight, remider));
	}

	/**
	 * 
	 * @param item
	 */
	public void addBasicItem(BasicItem item) {
		mItemList.add(item);
	}

	/**
	 * 
	 * @param itemView
	 */
	public void addViewItem(ViewItem itemView) {
		mItemList.add(itemView);
	}

	public void commit() {
		mIndexController = 0;

		if (mItemList.size() > 1) {
			// when the list has more than one item
			for (IHashItem obj : mItemList) {
				View tempItemView;
				if (mIndexController == 0) {
					tempItemView = mInflater.inflate(R.layout.hash_item_left, null);
				} else if (mIndexController == mItemList.size() - 1) {
					tempItemView = mInflater.inflate(R.layout.hash_item_right, null);
				} else {
					tempItemView = mInflater.inflate(R.layout.hash_item_middle, null);
				}
				setupItem(tempItemView, obj, mIndexController);
				tempItemView.setClickable(obj.isClickable());
				mListContainer.addView(tempItemView);
				LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT,
						LayoutParams.MATCH_PARENT, 1.0f);
				((RelativeLayout) tempItemView).setLayoutParams(params);
				mIndexController++;
			}
		} else if (mItemList.size() == 1) {
			// when the list has only one item
			View tempItemView = mInflater.inflate(R.layout.hash_item_single, null);
			IHashItem obj = mItemList.get(0);
			setupItem(tempItemView, obj, mIndexController);
			tempItemView.setClickable(obj.isClickable());
			mListContainer.addView(tempItemView);
		}
	}

	private void setupItem(View view, IHashItem item, int index) {
		if (item instanceof BasicItem) {
			BasicItem tempItem = (BasicItem) item;
			setupBasicItem(view, tempItem, mIndexController);
		} else if (item instanceof ViewItem) {
			ViewItem tempItem = (ViewItem) item;
			setupViewItem(view, tempItem, mIndexController);
		}
	}

	/**
	 * 
	 * @param view
	 * @param item
	 * @param index
	 */
	private void setupBasicItem(View view, BasicItem item, int index) {

		if (item.getSubtitle() != null) {
			((TextView) view.findViewById(R.id.summary)).setText(item.getSubtitle());
		} else {
			((TextView) view.findViewById(R.id.summary)).setVisibility(View.GONE);
		}
		((TextView) view.findViewById(R.id.title)).setText(item.getTitle());
		if (item.getColor() > -1) {
			((TextView) view.findViewById(R.id.title)).setTextColor(item.getColor());
		}
		view.setTag(index);
		if (item.isClickable()) {
			view.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View view) {
					if (mClickListener != null)
						mClickListener.onClick((Integer) view.getTag());
				}

			});
		} else {
			((View) view.findViewById(R.id.view_line)).setVisibility(View.GONE);
		}
	}

	/**
	 * 
	 * @param view
	 * @param itemView
	 * @param index
	 */
	private void setupViewItem(View view, ViewItem itemView, int index) {
		if (itemView.getView() != null) {
			RelativeLayout itemContainer = (RelativeLayout) view.findViewById(R.id.itemContainer);
			itemContainer.removeAllViews();
			// itemContainer.removeAllViewsInLayout();
			itemContainer.addView(itemView.getView());

			if (itemView.isClickable()) {
				itemContainer.setTag(index);
				itemContainer.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View view) {
						if (mClickListener != null)
							mClickListener.onClick((Integer) view.getTag());
					}
				});
			}
		}
	}

	public interface ClickListener {
		void onClick(int index);
	}

	/**
	 * 
	 * @return
	 */
	public int getCount() {
		return mItemList.size();
	}

	/**
	 * 
	 */
	public void clear() {
		mItemList.clear();
		mListContainer.removeAllViews();
	}

	/**
	 * 
	 * @param listener
	 */
	public void setClickListener(ClickListener listener) {
		this.mClickListener = listener;
	}

	/**
	 * 
	 */
	public void removeClickListener() {
		this.mClickListener = null;
	}

}
